#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System;

/// <summary>
/// SceneNameDrawer这个类是用来自定义SceneNameAttribute类型的属性在Inspector面板中的显示方式的
/// 自定义属性显示的逻辑
/// </summary>
[CustomPropertyDrawer(typeof(SceneNameAttribute))]
public class SceneNameDrawer : PropertyDrawer
{
    // int sceneIndex = -1; //当前选中的场景索引
    GUIContent[] sceneNames; //下拉框每个label

    readonly string[] scenePathSplit = { "/", ".unity" };

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        if (EditorBuildSettings.scenes.Length == 0) return;

        if (sceneNames == null || sceneNames.Length == 0) GetSceneNameArray(property);

        int sceneIndex = Array.FindIndex(sceneNames, x => x.text == property.stringValue);

        if (sceneIndex == -1)
        {
            sceneIndex = 0;
            property.stringValue = sceneNames[sceneIndex].text;
        }

        int oldIndex = sceneIndex;

        sceneIndex = EditorGUI.Popup(position, label, sceneIndex, sceneNames, EditorStyles.popup);

        if (oldIndex != sceneIndex)
        {
            property.stringValue = sceneNames[sceneIndex].text;
        }
    }


    private void GetSceneNameArray(SerializedProperty property)
    {
        var scenes = EditorBuildSettings.scenes;

        // 初始化
        sceneNames = new GUIContent[scenes.Length];
        for (int i = 0; i < scenes.Length; i++)
        {
            string path = scenes[i].path;
            string[] splicePath = path.Split(scenePathSplit, System.StringSplitOptions.RemoveEmptyEntries);

            string sceneName;
            if (splicePath.Length > 0)
            {
                sceneName = splicePath[splicePath.Length - 1];
            }
            else
            {
                sceneName = "{Deleted Scene}";
            }

            sceneNames[i] = new GUIContent(sceneName);
        }

        if (sceneNames.Length == 0)
        {
            sceneNames = new[] { new GUIContent("No Scene Found") };
        }
    }



    /// <summary>
    /// 
    /// </summary>
    /// <param name="position"></param>
    /// <param name="property">标记了SceneNameAttribute 的属性</param>
    /// <param name="label"></param>
    // public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    // {
    //     if (EditorBuildSettings.scenes.Length == 0) return;

    //     if (sceneIndex == -1) GetSceneNameArray(property);

    //     int oldIndex = sceneIndex;

    //     sceneIndex = EditorGUI.Popup(position, label, sceneIndex, sceneNames, EditorStyles.popup);

    //     if (oldIndex != sceneIndex){
    //         property.stringValue = sceneNames[sceneIndex].text;
    //     }
    // }

    // private void GetSceneNameArray(SerializedProperty property)
    // {
    //     var scenes = EditorBuildSettings.scenes;

    //     // 初始化
    //     sceneNames = new GUIContent[scenes.Length];
    //     for (int i = 0; i < scenes.Length; i++)
    //     {
    //         string path = scenes[i].path;
    //         string[] splicePath = path.Split(scenePathSplit, System.StringSplitOptions.RemoveEmptyEntries);

    //         string sceneName;
    //         if (splicePath.Length > 0)
    //         {
    //             sceneName = splicePath[splicePath.Length - 1];
    //         }
    //         else
    //         {
    //             sceneName = "{Deleted Scene}";
    //         }

    //         sceneNames[i] = new GUIContent(sceneName);
    //     }

    //     if (sceneNames.Length == 0)
    //     {
    //         sceneNames = new[] { new GUIContent("No Scene Found") };
    //     }

    //     if (!string.IsNullOrEmpty(property.stringValue))
    //     {
    //         bool nameFound = false;

    //         for (int i = 0; i < sceneNames.Length; i++)
    //         {
    //             if (sceneNames[i].text == property.stringValue)
    //             {
    //                 sceneIndex = i;
    //                 nameFound = true;
    //                 break;
    //             }
    //         }
    //         if (!nameFound) sceneIndex = 0;
    //     }
    //     else
    //     {
    //         sceneIndex = 0;
    //     }

    //     //设置具体值
    //     property.stringValue = sceneNames[sceneIndex].text;
    // }

}

#endif